/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.managers;

import opiam.admin.faare.MessageUtil;

import org.apache.log4j.Logger;

import java.io.IOException;

import java.util.Properties;


/**
 * This class loads application parameters from application properties file (located in $CLASSPATH/config).
 */
public final class AppliParametersManager
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(AppliParametersManager.class);

    /** Current instance of AppliParametersManager. */
    private static AppliParametersManager _instance = null;

    /** Application proprties file. */
    private static final String APPLICATION_PROPS_FILE = "/config/application.properties";

    /** Application properties. */
    private Properties appliProperties = new Properties();

    //DW/2675//BeginPatch
    /** Max logon trial. */
    private static final String maxLogonTrial_param = "param.maxlogontrial";
    //DW/2675//EndPatch

    /** Max logon trial ( if (maxLogonTrial <= 0) then there is no limit. */
    private int maxLogonTrial = 0;

    /**
     * Creates a new AppliParametersManager object.
     *
     */
    private AppliParametersManager()
    {
    }

    /**
     * Loads application parameters from application.properties file.
     */
    public static void initialize()
    {
        _instance = new AppliParametersManager();

        try
        {
            _instance.appliProperties.load(_instance.getClass()
                                                    .getResourceAsStream(APPLICATION_PROPS_FILE));
            _logger.info(APPLICATION_PROPS_FILE + " succesfully loaded");
        }
        catch (IOException ioe)
        {
            _logger.error(MessageUtil.formatMessage("MSG_FILE_ERROR",
                    APPLICATION_PROPS_FILE));
        }

        //DW/2675//BeginPatch
        //String maxLogonTrialStr = _instance.appliProperties.getProperty("tutorial.param.maxlogontrial");
        String maxLogonTrialStr = _instance.appliProperties.getProperty(maxLogonTrial_param);
        //DW/2675//EndPatch

        try
        {
            _instance.maxLogonTrial = Integer.parseInt(maxLogonTrialStr);
        }
        catch (NumberFormatException nfe)
        {
            _logger.error(nfe);
        }

        _logger.debug("***** maxLogonTrial = " + _instance.maxLogonTrial);

        _logger.info(".. AppliParametersManager.initialize() <end>");
    }

    /**
     * Gets current instance of AppliParametersManager.
     * initialize() must be called first.
     *
     * @return instance
     */
    public static AppliParametersManager getInstance()
    {
        if (_instance == null)
        {
            _logger.error(
                "Should call AppliParametersManager.initialize() first !");
            throw new RuntimeException(
                "Should call AppliParametersManager.initialize() first !");
        }

        return _instance;
    }

    /**
     * Returns the application properties.
     * @return application properties
     */
    public Properties getAppliProperties()
    {
        return appliProperties;
    }

    /**
     * Gets an application property.
     *
     * @param key property name
     *
     * @return property value
     */
    public String getAppliProperty(String key)
    {
        return appliProperties.getProperty(key);
    }

    /**
     * Returns the max logon trial.<br>
     * 0 means no limit.<br>
     * Default value is 0.
     * @return max logon trial
     */
    public int getMaxLogonTrial()
    {
        return maxLogonTrial;
    }

    /**
    * Returns true if max logon trial > 0.
    * @return int
    */
    public boolean hasLogonTrialLimit()
    {
        return (maxLogonTrial > 0);
    }
}
